<?php if (!defined('THINK_PATH')) exit();?>      <style type="text/css">
       #main_img:hover{
         background: #ccc;
         color:#ccc;
       }
      </style>
      <p class="user_title" style="height: 40px;line-height: 40px;border-bottom: 1px solid #fff;">
        <span style="font-size: 20px; padding-left: 12px;"><?php echo ($data); ?></span>
      </p>
     
      <form class="form-horizontal" enctype="multipart/form-data">
       <input id="fileupload" type="file" name="mypic" style="display:none" onchange="ajax_upload()" /> 
      <div style="display: block; float:right; z-index:10;" id="main_img">
      <a href="###" id="img_head">
      <img src="/mydemo/Public/<?php echo ($user["head"]); ?>" width="100" height="100"   class="img-thumbnail" alt="Responsive image">
      <p style="text-align:center;color:#333;z-index:9;" class="">头像设置</p>
      </a>
      </div>
    <div class="form-group">
    <label for="msg" class="col-sm-2 control-label"></label>
    <div class="col-sm-4">
     <span id="msg" style="color:red;font-size:20px; display:none"></span>
    </div>
  </div>
  <div class="form-group">
    <label for="username" class="col-sm-2 control-label">帐号名称：</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="username" value="<?php echo ($user["user_name"]); ?>" >
    </div>
  </div>
  <div class="form-group">
    <label for="email" class="col-sm-2 control-label">电子邮箱：</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" id="email" value="<?php echo ($user["email"]); ?>">
    </div>
  </div>
    <div class="form-group">
    <label for="motto" class="col-sm-2 control-label">个人简介：</label>
    <div class="col-sm-6">
      <textarea class="form-control" id="motto" rows="3"><?php echo ($user["motto"]); ?></textarea>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" id="submit" class="btn btn-default">保存</button>
    </div>
  </div>
</form>
<script type="text/javascript">
$(function(){
     $('#main_img').bind('click',img_head);
     $('#submit').bind('click',sub_message);
 });

function img_head(){
  $('#fileupload').click();
}


function ajax_upload(){
              var imgPath = $("#fileupload").val();
            if (imgPath == "") {
                alert("请选择上传图片！");
                return;
            }
            //判断上传文件的后缀名
            var strExtension = imgPath.substr(imgPath.lastIndexOf('.') + 1);
            if (strExtension != 'jpg' && strExtension != 'gif'
            && strExtension != 'png' && strExtension != 'bmp') {
                alert("请选择图片文件");
                return;
            }
            $.ajax({
                type: "POST",
                url: "<?php echo u('ajax_upload');?>",
                data: $('.form-horizontal').serialize(),
                cache: false,
                success: function(data) {
                    alert("上传成功");
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert("上传失败，请检查网络后重试");
                }
            });
        }

function sub_message(){
          var name= $('#username').val();
          var email= $('#email').val();
          var motto= $('#motto').val();
          if(name ==""){
            $('#msg').html('名字不能为空!').show();
            return;
          }
           var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
            if(!reg.test(email)){
             $('#msg').html('邮箱格式不正确!').show();
             return;
            }
         if(motto == ""){
           $('#msg').html('内容不能为空!').show();
           return;
         }

               $.ajax({
                 url: "<?php echo u('User/save');?>",
                 type: 'POST',
                 dataType: 'json',
                 data: {"name": name,
                        "email" : email,
                        "motto" : motto
                 },
                 complete: function(xhr, textStatus) {
                   //called when complete
                 },
                 success: function(data) {
                   if(data.status == 1){
                    $('#msg').html(data.msg).css('color','green').show();
                    setTimeout(function(){
                       $('#msg').html("").hide().css('color','red');
                    },1000);
                   }else{
                    $('#msg').html(data.msg).show();
                    setTimeout(function(){
                       $('#msg').html("").hide();
                    },1000);
                   }
                 },
                 error: function(xhr, textStatus, errorThrown) {
                   //called when there is an error
                 }
             });
 }

</script>